package com.zhiquan.cai.controller;

import com.zhiquan.cai.entity.User;
import com.zhiquan.cai.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/Admin")
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 访问登录页
     * @return
     */
    @RequestMapping("/login")
    public String login() {
        return "admin/login";
    }

    /**
     * 访问首页
     * @return
     */
    @RequestMapping("/index")
    public String index() {
        return "admin/index";
    }

    /**
     * 登录处理
     */
    @RequestMapping("/doLogin")
    public String doLogin(String username, String password, Model model) {
        // 使用Shiro编写认证处理
        // 1、获取Subject
        Subject subject = SecurityUtils.getSubject();

        // 2、封装用户数据
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);

        // 3、执行登录
        try {
            // 登录成功
            subject.login(token);

            // 返回当前用户的帐号
            model.addAttribute("currentuser", token.getUsername());

            return "admin/index";
        } catch (UnknownAccountException exception) {
            // 返回错误信息
            model.addAttribute("msg", "账号错误！");

            return "admin/login";
        } catch (IncorrectCredentialsException exception) {
            // 返回错误信息
            model.addAttribute("msg", "密码错误！");

            return "admin/login";
        }
    }

    /**
     * 注销处理
     * @return
     */
    @RequestMapping("/doLogout")
    public String doLogout() {
        // 1、获取Subject
        Subject subject = SecurityUtils.getSubject();

        // 2、执行注销
        try {
            subject.logout();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return "admin/login";
        }
    }

    /**
     * 用户管理
     * @return
     */
    @RequestMapping("/user")
    public String user(@RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "2") int pageSize, Model model) {
        Pageable pageable = PageRequest.of(pageNo - 1,pageSize, Sort.Direction.DESC,"userid");
        Page<User> userPage = this.userService.userGetAll(pageable);
        long totalElements = userPage.getTotalElements();
        long totalPages = userPage.getTotalPages();
        List<User> listInfo = userPage.getContent();
        Map map = new HashMap<String,Object>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("pageSize", pageSize);
        map.put("currentPage", pageNo);
        map.put("listInfo", listInfo);
        model.addAttribute("userPage",map);
        return "admin/user";
    }

    /**
     * 添加用户页面
     * @return
     */
    @RequestMapping("/userAdd")
    public String userAdd() {
        return "admin/userAdd";
    }

    /**
     * 添加用户方法
     */
    @PostMapping("/userAddAction")
    public String userAddAction(HttpServletRequest request){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        try{
            this.userService.userAdd(user);
            return "redirect:/Admin/user";
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }
    }
}